// miniprogram/pages/bar/index/index.js
const APIUrl = require('../../../tools/APIurl');
const mAPIUrl = new APIUrl()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    backgroundImg: [
      { img: '/images/bankcard.png' },
      { img: '/images/rmb.png' },
      { img: '/images/service.png' },
      { img: '/images/usage.png' },
    ],
    menu:[
      { img: '/images/bankcard.png' , text:'租约' ,managerEvents:'managersLease',userEvents:'userLease'},
      { img: '/images/rmb.png' , text:'账单' ,managerEvents:'managersBelas',userEvents:'userBelas'},
      { img: '/images/service.png' , text:'智能设备' ,managerEvents:'lease',userEvents:''},
      { img: '/images/usage.png' , text:'更多' ,managerEvents:'lease',userEvents:''},
    ],
    userInfo:{
      avatarUrl: '', //用户头像
      nickName: '', //用户昵称
      identify:false,//用户身份
      managersBelas:'managersBelas',
      userBelas:'userBelas',
    }, 
    remind:{
      ledgerNumber:0,
      ledgerEvent:'',
      futureBill:0,
      contractNumber:0,
      contractEvent:'',
      timeout:0,
    },
    bill:{
      realIncome:'0',
      futureIncome:'0',
      realSpend:'0',
      futureSpend:'0',
    },
  },
  managersBelas(){
    wx.navigateTo({
      url: './managers/belas/belas',
    })
  },
  userBelas(){
    wx.navigateTo({
      url: './user/belas/belas',
    })
  },
  managersLease(){
    wx.navigateTo({
      url: './managers/lease/lease',
    })
  },
  userLease(){
    wx.navigateTo({
      url: './user/lease/lease',
    })
  },
  onLoad: function (options) {
    that = this
    var userInfo = that.data.userInfo
    userInfo.identify = wx.getStorageSync('userInfo').type
    var year=new Date().getFullYear()
    var month=new Date().getMonth()+1
    var day = new Date().getDate()
    that.setData({
      userInfo:userInfo
    })
    // 获取当月账单
    if(wx.getStorageSync('userInfo').type==true){
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listBill',
        landlord_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var remind = that.data.remind
        var receivedTotal = 0
        var bill = that.data.bill
        remind.ledgerNumber=res.length
        remind.futureBill=res.length
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.futureIncome=receivedTotal
        that.setData({
          remind:remind,
          bill:bill
        })
      })
      // 获取当月已收到账单
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listReceivedBill',
        landlord_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var receivedTotal = 0
        var bill = that.data.bill
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.realIncome=receivedTotal
        that.setData({
          bill:bill,
        })
      })
    }else{
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listGuestBill',
        guests_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var remind = that.data.remind
        var receivedTotal = 0
        var bill = that.data.bill
        remind.ledgerNumber=res.length
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.futureSpend=receivedTotal
        that.setData({
          remind:remind,
          bill:bill
        })
      })
      // 获取当月已收到账单
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listGuestReceivedBill',
        guests_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var receivedTotal = 0
        var bill = that.data.bill
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.realSpend=receivedTotal
        that.setData({
          bill:bill,
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this
    var userInfo = that.data.userInfo
    userInfo.identify = wx.getStorageSync('userInfo').type
    var year=new Date().getFullYear()
    var month=new Date().getMonth()+1
    var day = new Date().getDate()
    that.setData({
      userInfo:userInfo
    })
    // 获取当月账单
    if(wx.getStorageSync('userInfo').type==true){
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listBill',
        landlord_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var remind = that.data.remind
        var receivedTotal = 0
        var bill = that.data.bill
        remind.ledgerNumber=res.length
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.futureIncome=receivedTotal
        that.setData({
          remind:remind,
          bill:bill
        })
      })
      // 获取当月已收到账单
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listReceivedBill',
        landlord_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var receivedTotal = 0
        var bill = that.data.bill
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.realIncome=receivedTotal
        that.setData({
          bill:bill,
        })
      })
    }else{
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listGuestBill',
        guests_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var remind = that.data.remind
        var receivedTotal = 0
        var bill = that.data.bill
        remind.ledgerNumber=res.length
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.futureSpend=receivedTotal
        that.setData({
          remind:remind,
          bill:bill
        })
      })
      // 获取当月已收到账单
      mAPIUrl.list('/api/mgr/bill',{
        action: 'listGuestReceivedBill',
        guests_id: wx.getStorageSync('userInfo').id,
        end_time:year+'-'+(month < 10 ? '0' + month : month)
      },function(res){
        console.log(res)
        var receivedTotal = 0
        var bill = that.data.bill
        for(var i =0;i<res.length;i++){
          receivedTotal += parseFloat(res[i].all_cost)
        }
        bill.realSpend=receivedTotal
        that.setData({
          bill:bill,
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})